﻿// Copyright 2009 Mike Geise
// 
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// 
//     http://www.apache.org/licenses/LICENSE-2.0
// 
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

namespace Falcon.Forums.Domain.Extensions
{
	using System.Collections.Generic;
	using System.Linq;
	using Falcon.Core.Authorization;
	using Falcon.Core.Domain.Enums;
	using Falcon.Core.Extensions;
	using Falcon.Forums.Domain.Enums;
	using Falcon.Forums.Domain.Values;

	public static class ForumPermissionExtension
	{
		/// <summary>
		/// Groups the permissions by role.
		/// </summary>
		/// <param name="permissions">The permissions.</param>
		/// <returns></returns>
		public static Dictionary<string, ForumPermission> GroupByRole(this IList<ForumPermission> permissions)
		{
			var keyed = new Dictionary<string, ForumPermission>();

			foreach (var permission in permissions)
			{
				keyed.Add(permission.Role.Title, permission);
			}

			return keyed;
		}

		/// <summary>
		/// Determines whether the specified actions [has] [the specified action].
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if the specified actions [has] [the specified action]; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(ForumPermissionActions actions, int action)
		{
			return (((int)actions & action) == action);
		}

		/// <summary>
		/// Determines whether the specified actions [has] [the specified action].
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if the specified actions [has] [the specified action]; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(ForumPostPermissionActions actions, int action)
		{
			return (((int)actions & action) == action);
		}

		/// <summary>
		/// Determines whether the specified actions [has] [the specified action].
		/// </summary>
		/// <param name="actions">The actions.</param>
		/// <param name="action">The action.</param>
		/// <returns>
		/// 	<c>true</c> if the specified actions [has] [the specified action]; otherwise, <c>false</c>.
		/// </returns>
		public static bool Has(ForumTopicPermissionActions actions, int action)
		{
			return (((int)actions & action) == action);
		}

		/// <summary>
		/// Gets the calculated action value from the specified forum permission values.
		/// </summary>
		/// <param name="values">The values.</param>
		/// <returns></returns>
		public static int Value(this ForumPermissionValue[] values)
		{
			var action = 0;

			for (var i = 0; i < values.Length; i++)
			{
				if (!values[i].Selected)
				{
					continue;
				}

				action |= values[i].Action;
			}

			return action;
		}
	}
}